package org.dingyom.gift.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;

/**
 * Oauth2资源配置
 */
@Configuration
//开启资源服务配置
@EnableResourceServer
//开启方法授权
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ResourceServerConfigurerAdapter extends BaseResourceServiceConfig {
    @Override
    public void configure(HttpSecurity httpSecurity) throws Exception {

        //springsecurity放行等配置
        httpSecurity.authorizeRequests()
                //登录放行
                .antMatchers("/loginUser/login/**").permitAll()
                //校验scope必须为gift ， 对应认证服务的客户端详情配置的clientId
                .antMatchers("/**").access("#oauth2.hasScope('gift')")
                //关闭跨域伪造检查
                .and().csrf().disable()
                //把session设置为无状态，意思是使用了token，那么session不再做数据的记录
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }


}